<?php
/*
	Copyright © Eleanor CMS
	URL: http://eleanor-cms.ru, http://eleanor-cms.com
	E-mail: support@eleanor-cms.ru
	Developing: Alexander Sunvas*
	Interface: Rumin Sergey
	=====
	*Pseudonym
*/
if(!defined('CMS'))die;
global$Eleanor;
$Eleanor->module['config']=include($Eleanor->module['path'].'config.php');
Eleanor::LoadOptions($Eleanor->module['config']['opts']);

$ev=isset($_POST['event']) ? (string)$_POST['event'] : '';
switch($ev) {
	case'comments':
			$id=isset($_POST['id']) ? (int)$_POST['id'] : 0;
			$R = Eleanor::$Db->Query('SELECT `id`,`cats`,`uri` FROM `'.$Eleanor->module['config']['t'].'` INNER JOIN `'.$Eleanor->module['config']['tl'].'` USING(`id`) WHERE `id`='.$id.' AND `language`IN(\'\',\''.Language::$main.'\') AND `status`=1 LIMIT 1');
			if(!$a=$R->fetch_assoc())
				return Error();
			BeAs('user');

			$Eleanor->Categories->Init($Eleanor->module['config']['c']);
			$cat=$a['cats'] && $Eleanor->Url->furl ? $Eleanor->Categories->GetUri((int)ltrim($a['cats'],',')) : false;
			$u=array('u'=>array($a['uri'],'nid'=>$a['id']));
			$data=isset($_POST['comments']) ? (array)$_POST['comments'] : array();
			$Eleanor->Comments_ajax->baseurl=array('module'=>$Eleanor->module['name'])+($cat ? $cat+$u : $u);
			if($r=$Eleanor->Comments_ajax->Process($data,$id))
				switch($r['event'])
				{
					case'post':
					
						if($Eleanor->Comments->rights['post']==1 and !$r['merged'])
							Eleanor::$Db->Update($Eleanor->module['config']['t'],array('!comments'=>'`comments`+1'),'`id`='.$id.' LIMIT 1');
					case'save':
						Eleanor::$Db->Update($Eleanor->module['config']['tl'],array('!last_mod'=>'NOW()'),'`id`='.$id.' LIMIT 1');
					break;
					case'delete':
						if($r['deleted'])
						{
							Eleanor::$Db->Update($Eleanor->module['config']['t'],array('!comments'=>'IF(`comments`>'.$r['deleted'].',`comments`-'.$r['deleted'].',0)'),'`id`='.$id.' LIMIT 1');
							Eleanor::$Db->Update($Eleanor->module['config']['tl'],array('!last_mod'=>'NOW()'),'`id`='.$id.' LIMIT 1');
						}
					break;
					case'moderate':
						if($r['activated'])
						{
	Eleanor::$Db->Update($Eleanor->module['config']['t'],array('!comments'=>'GREATEST(0,`comments`'.($r['activated']>0 ? '+'.$r['activated'] : $r['activated']).')'),'`id`='.$id.' LIMIT 1');
							Eleanor::$Db->Update($Eleanor->module['config']['tl'],array('!last_mod'=>'NOW()'),'`id`='.$id.' LIMIT 1');
						}
				}
	break;
	default:
		Error(Eleanor::$Language['main']['unknown_func']);
}